global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/dep_vars.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"

capture noi {

qui do ${code_dir}/config/labeling_finalvars.do


***********************************************************************************************************************
* dep_vars.do
* v2
/* This do-file load the classification from patent_lists 
and creates the firm-level dependent variables and stock */

* Create the firm-level patent counts and stocks (dependent variables)
* Input: automation and placebo patents in automation/patent_lists/
* Output: ./dep_vars/bvd_year_depvars.dta
***********************************************************************************************************************

************************************
* Dependent variables
************************************
*import firms and their patents
use ${dataset_dir}/patstat_orbis/Orbis_patents_list_2017_merged.dta, clear
keep if ipr_type==2
mmerge appln_id using ${commondata_dir}/patstat_2018b/appln_info.dta, unmatched(master) ukeep(granted)
keep BvDIDnumber appln_id granted

*merge in technologies
foreach xxx in auto80 auto90 auto95 autoX95 autonol95 pauto90 pauto95 pauto90_rm6 CNC80 CNC90 CNC95 robo80 robo90 robo95 autm80 autm90 autm95 {
	mmerge appln_id using ${dataset_dir}/patent_list/`xxx'_patents.dta, unmatched(master)
	gen `xxx'=(_m==3)
}
*merge in and create marker for machinery
mmerge appln_id using ${dataset_dir}/patent_list/pats_tfa.dta, unmatched(master)
gen tfa = _m == 3

*merge in patent family information
mmerge appln_id using ${commondata_dir}/patstat_2018b/family_info.dta, unmatched(master) ukeep(docdb_family_id fam_earliest_appln_year )

*if at least one application is of the technology, set dummy for patent family and technology to 1
ren fam_earliest_appln_year inv_year 
foreach xxx in auto80 auto90 auto95 autoX95 autonol95 pauto90 pauto95 pauto90_rm6 CNC80 CNC90 CNC95 robo80 robo90 robo95 autm80 autm90 autm95 tfa {
	bys docdb_family_id : egen fam_`xxx' = max(`xxx')
}
*merge in biadic information
mmerge docdb_family_id using ${dataset_dir}/patstat_orbis/docdb_families2.dta, unmatched(master) ukeep(biadic_D)
ren biadic_D bia

keep BvD inv_year docdb_family_id fam_* bia
duplicates drop

*count biadic patent families by technology and (first filing) year
gen fam_any=1
foreach xxx in auto80 auto90 auto95 autoX95 autonol95 pauto90 pauto95 pauto90_rm6 CNC80 CNC90 CNC95 robo80 robo90 robo95 autm80 autm90 autm95 tfa any {
	bys BvD inv_year : egen `xxx'_bia = sum(fam_`xxx'*bia)
}


keep BvD inv_year auto*_* p*_* any_* tf* CNC??_* robo??_* autm??_*
duplicates drop
*label variables
label var BvD "current owners firm id"
label var inv_year "year of first patent application"


compress
save ${dataset_dir}/dep_vars/bvd_year_depvars.dta, replace

************************************
* Stocks
************************************

gen YY=inv_year>1979 & inv_year<2017
*count all biadic patents in a given technology by a firm inside the period
foreach xxx in auto80 auto90 auto95 autoX95 autonol95 pauto90 pauto95 pauto90_rm6 CNC80 CNC90 CNC95 robo80 robo90 robo95 autm80 autm90 autm95 tfa any {
	bys BvD : egen total_`xxx'_bia = sum(`xxx'_bia*YY)
}
*drop firms that do not have any patents in our categories during our sample period. so drop firms that can have any other type of patents.
egen allinnovtypes=rowtotal(total_*)
drop if allinnovtypes==0
drop allinnovtypes total_* YY
keep if inv_year>1949 & inv_year<2017
*complete the panel
fillin BvD inv_year
drop _fillin
foreach xxx in auto80 auto90 auto95 autoX95 autonol95 pauto90 pauto95 pauto90_rm6 CNC80 CNC90 CNC95 robo80 robo90 robo95 autm80 autm90 autm95 tfa any {
		replace `xxx'_bia = 0 if `xxx'_bia==.
}
ren inv_year year

*make a numeric only identifier to make xtset work
egen lse_id=group(BvD)
xtset lse_id year, yearly

*generate counts of biadic patents NOT in a given technology
foreach xxx in auto80 auto90 auto95 autoX95 autonol95 pauto90 pauto95 pauto90_rm6 CNC80 CNC90 CNC95 robo80 robo90 robo95 autm80 autm90 autm95 tfa any {
		gen NOT_`xxx'_bia = any_bia - `xxx'_bia
		replace NOT_`xxx'_bia=0 if NOT_`xxx'_bia<0	
}

*stocks of biadic patents are calculated as the sum of the current year and 85% of the previous years stock ("k" as usual as in "kapital")
foreach pt of varlist *bia {
	* stocks
	gen k`pt'=`pt'
	bysort lse_id (year): replace k`pt'=k`pt'+0.85* k`pt'[_n-1] if _n>1
	gen byte k`pt'0 = k`pt'==0
	replace k`pt'=ln(k`pt') 
	replace k`pt'= 0 if k`pt'0 == 1
}


*labeling
foreach xxx in auto80 auto90 auto95 autoX95 autonol95 pauto90 pauto95 pauto90_rm6 CNC80 CNC90 CNC95 robo80 robo90 robo95 autm80 autm90 autm95 tfa any {
labelingvarsfinal "`xxx'"
}

keep if year>1979 
compress
save ${dataset_dir}/dep_vars/bvd_year_depvars.dta, replace
keep BvD year
save ${dataset_dir}/dep_vars/bvd_year_depvars_list.dta, replace

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat